package book;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 19182
 * Date: 2025-07-03
 * Time: 18:09
 */
public class Book implements Comparable<Book>{
    private int bookId;
    private String title;//书名
    private String author;  //作者
    private String category; //类别
    private int publishYear; //出版年份
    private boolean isBorrowed; //是否被借出去
    private int borrowedCount; //被借阅的次数
    private LocalDate shelDate; //上架时间

    public Book(String title, String author, String category, int publishYear, LocalDate shelDate) {
        this.title = title;
        this.author = author;
        this.category = category;
        this.publishYear = publishYear;
        this.shelDate = shelDate;
    }

    public int getBookId() {
        return bookId;
    }

    public void setBookId(int bookId) {
        this.bookId = bookId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public int getPublishYear() {
        return publishYear;
    }

    public void setPublishYear(int publishYear) {
        this.publishYear = publishYear;
    }

    public boolean isBorrowed() {
        return isBorrowed;
    }

    public void setBorrowed(boolean borrowed) {
        isBorrowed = borrowed;
    }

    public int getBorrowedCount() {
        return borrowedCount;
    }

    public void setBorrowedCount(int borrowedCount) {
        this.borrowedCount = borrowedCount;
    }

    public LocalDate getShelDate() {
        return shelDate;
    }

    public void setShelDate(LocalDate shelDate) {
        this.shelDate = shelDate;
    }
    public void incrementBorrowCount(){
        this.borrowedCount++;   //借阅次数增加1
    }

    @Override
    public String toString() {
        return "Book{" +
                "bookId=" + bookId +
                ", title='" + title + '\'' +
                ", author='" + author + '\'' +
                ", category='" + category + '\'' +
                ", publishYear=" + publishYear +
                ", isBorrowed=" + isBorrowed +
                ", borrowedCount=" + borrowedCount +
                ", shelDate=" + shelDate +
                '}';
    }

    public void decreaseBorrowCount(){
        this.borrowedCount--;
    }

    public String toJson() {    //把Book对象转换成字符串
        StringBuilder json = new StringBuilder();
        json.append(bookId).append(",");
        json.append(title).append(",");
        json.append(author).append(",");
        json.append(category).append(",");
        json.append(publishYear).append(",");
        json.append(isBorrowed).append(",");
        json.append(borrowedCount).append(",");
        json.append(shelDate != null ? shelDate.format(DateTimeFormatter.ISO_LOCAL_DATE) : "null");
        //这里的format方法去调用DateTimeFormatter类中的静态常量会把我们的shelDate格式化成yyyy-MM-dd格式的字符串
        return json.toString();
    }

    @Override
    public int compareTo(Book o) {
        return o.getBorrowedCount() - this.getBorrowedCount();  //最受欢迎的前k本书肯定是借阅次数最多
    }
}
